package com.ruoyi.mateCheck.controller;

import com.alibaba.fastjson.JSONArray;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.domain.MateCheck;
import com.ruoyi.mateCheck.domain.MateCheckModel;
import com.ruoyi.mateCheck.domain.MateCheckResult;
import com.ruoyi.mateCheck.domain.MateTest;
import com.ruoyi.mateCheck.mapper.MateCheckMapper;
import com.ruoyi.mateCheck.mapper.MateTestMapper;
import com.ruoyi.mateCheck.service.MateCheckService;
import com.ruoyi.mateDevice.domain.MateDevice;
import com.ruoyi.mateDevice.mapper.MateDeviceMapper;
import com.ruoyi.mateInventory.domain.MateInventory;
import com.ruoyi.mateInventory.service.MateInventoryService;
import com.ruoyi.material.domain.MateProductDevice;
import com.ruoyi.material.domain.MaterialProduct;
import com.ruoyi.material.mapper.MateProductMapper;
import com.ruoyi.material.service.MateProductService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import com.ruoyi.mtAudit.service.MtAuditService;
import com.ruoyi.product.domain.ProductRelation;
import com.ruoyi.product.mapper.ProductMapper;
import com.ruoyi.readyProcurement.domain.ReadyProcurement;
import com.ruoyi.readyProcurement.domain.ReadyProcurementModel;
import com.ruoyi.readyProcurement.mapper.ReadyMapper;
import com.ruoyi.readyProcurement.service.ReadyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/mateCheck")
public class MateCheckController extends BaseController {

    @Autowired
    MateCheckService mateCheckService;

    @Autowired
    MateProductService mateProductService;

    @Autowired
    MateProductMapper mateProductMapper;

    @Autowired
    MateCheckMapper mateCheckMapper;

    @Autowired
    MtAuditService mtAuditService;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MateDeviceMapper mateDeviceMapper;

    @Autowired
    MateInventoryService mateInventoryService;

    @Autowired
    ReadyService readyService;

    @Autowired
    ReadyMapper readyMapper;

    @Autowired
    ProductMapper productMapper;

    @Autowired
    MateTestMapper mateTestMapper;

    /**
     * 新增保存原材料检验记录
     */
    @Log(title = "保存原材料检验记录", businessType = BusinessType.INSERT)
    @PostMapping("/addCheckInformation")
    public AjaxResult addCheckInformation(@RequestBody MateCheck mateCheckInformation) {
        mateCheckInformation.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int a = mateCheckService.addCheckInformation(mateCheckInformation);
        if (a > 0) {
            MtAudit mtAudit = new MtAudit();
            mtAudit.setOrderId(mateCheckInformation.getCkInformationId());
            mtAudit.setCompanyId(SecurityUtils.getLoginUser().getCompanyId());
            mtAudit.setOrderType("0");
            mtAudit.setOrderNum(mateCheckInformation.getCheckOrdernum());
            mtAudit.setAuditStatus("0");
            mtAudit.setZjName(mateCheckInformation.getZjName());
            mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
            mtAuditService.insertMtAudit(mtAudit);
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    @GetMapping("/selectMateInventory")
    public Map<String, Object> selectMateInventory() {
        Map<String, Object> resMap = new HashMap<>();
        List<MateInventory> list = mateInventoryService.selectMateInventoryModel(SecurityUtils.getDeptId());
        resMap.put("list", list);
        return resMap;
    }

/*    @GetMapping("/selectRecord")
    public Map<String, Object> selectRecord(@RequestParam(value = "mateproductId", required = false) Long mateproductId) {
        Map<String, Object> resMap = new HashMap<>();
        if (mateproductId != null && mateproductId != 0) {
            MateCheck mateCheck = new MateCheck();
            mateCheck.setDeptId(SecurityUtils.getDeptId());
            mateCheck.setMateproductId(mateproductId);
            MateCheck mateCheck1 = mateCheckService.selectMateCheckById(mateCheck);
            if (mateCheck1 != null) {
                List<MateCheckModel> mateCheckModels = mateCheckMapper.selectMateCheckModelById(mateCheck1.getCkInformationId());
                MateDeviceModel mateDeviceModel = new MateDeviceModel();
                mateDeviceModel.setMateproductId(mateproductId);
                mateDeviceModel.setDeptId(SecurityUtils.getDeptId());
                List<MateDeviceModel> list = mateCheckMapper.selectMateDeviceModel(mateDeviceModel);
                resMap.put("result", mateCheckModels);
                resMap.put("mateDeviceModel", list);
                resMap.put("code", 200);
            } else {
                MateDeviceModel mateDeviceModel = new MateDeviceModel();
                mateDeviceModel.setMateproductId(mateproductId);
                mateDeviceModel.setDeptId(SecurityUtils.getDeptId());
                List<MateCheckModel> MateCheckModel = new ArrayList<>();
                List<MateDeviceModel> list = mateCheckMapper.selectMateDeviceModel(mateDeviceModel);//查询设备设置的检验记录模板
                if (list.size() != 0) {
                    resMap.put("result", MateCheckModel);
                    resMap.put("mateDeviceModel", list);
                    resMap.put("code", 200);
                } else {
                    resMap.put("code", -1);
                    resMap.put("result", "没有在设备管理中设置需要检验的信息记录");
                }
            }
        } else {
            resMap.put("code", -1);
            resMap.put("result", "原材料信息有误,请检查原材料信息是否正确");
        }
        return resMap;
    }*/

    /*
     * 查询原材料检验记录列表
     */
    @GetMapping("/checkInformation")
    @ResponseBody
    public TableDataInfo checkInformation(@RequestParam(value = "beginTime", required = false) String beginTime,
                                          @RequestParam(value = "endTime", required = false) String endTime) {

        MateCheck mateCheck = new MateCheck();
        mateCheck.setBeginTime(beginTime);
        mateCheck.setEndTime(endTime);
        startPage();
        mateCheck.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<MateCheck> list = mateCheckService.selectCheckInformation(mateCheck);
        return getDataTable(list);
    }

    /*
     查询已经审核通过的预入库管理
      */
    @GetMapping("/auditReadyProcurement")
    public Map<String, Object> auditCourse() {
        Map<String, Object> resMap = new HashMap<>();
        List<ReadyProcurement> readyProcurements = readyService.auditReadyProcurement(SecurityUtils.getDeptId());
        resMap.put("result", readyProcurements);
        return resMap;
    }

    /*
   点击编辑时查询当前原材料的信息
     */
    @GetMapping("/selectProductDevice")
    public Map<String, Object> selectProductDevice(@RequestParam(value = "productId", required = false) Long productId) {
        Map<String, Object> resMap = new HashMap<>();
        Map<String, Object> map = new HashMap<>();
        map.put("productId", productId);
        map.put("deptId", SecurityUtils.getDeptId());
        List<ProductRelation> productRelations = productMapper.selectMateProductId(map);
        List<MateProductDevice> list = new ArrayList<>();
        List<List<MateProductDevice>> mateProductDevices = new ArrayList<>();
        for (ProductRelation productRelation : productRelations) {
            list = mateProductMapper.selectMateProductDevice(productRelation.getMateproductId());
            mateProductDevices.add(list);
        }
        resMap.put("mateProductDevices", mateProductDevices);
        return resMap;
    }


    /*
    点击编辑时查询当前原材料的信息
      */
    @GetMapping("/selectEdit")
    public MateCheck selectCheckInformationById(@RequestParam(value = "ckInformationId", required = false) Long ckInformationId) {
        MateCheck mateCheck = mateCheckService.selectCheckInformationById(ckInformationId);
        mateCheck.setNum(mateCheck.getMateChcekModels().get(0).getCheckNum());
        JSONArray jsonArray = new JSONArray(Collections.singletonList(mateCheck.getInformationList()));
        mateCheck.setOtherInformation(jsonArray);
        return mateCheck;
    }

    @GetMapping("/selectProductModelLists")
    @ResponseBody
    public List<MaterialProduct> selectProductModelLists() {
        startPage();
        Long deptId = SecurityUtils.getLoginUser().getUser().getCompanyId();
        MaterialProduct product = new MaterialProduct();
        product.setCompanyId(deptId);
        return mateProductService.selectProductLists(product);
    }

    /*
    编辑已经做完的原材料检验记录
     */
    @PostMapping("/editCheckInformation")
    @ResponseBody
    public AjaxResult editCheckInformation(@RequestBody MateCheck mateCheck) {
        int a = mateCheckService.updateCheckInformation(mateCheck);
        if (a > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }

    @GetMapping("/remove/{ckInformationId}")
    public AjaxResult removeProcurement(@PathVariable("ckInformationId") Long ckInformationId) {
        MateCheck mateCheck = mateCheckMapper.selectMateCheckByOrderId(ckInformationId);
        ReadyProcurement readyProcurement = new ReadyProcurement();
        readyProcurement.setIsCreatCheck("0");
        readyProcurement.setReadyId(mateCheck.getReadyId());
        readyMapper.updateReadyProcurement(readyProcurement);
        mateCheckMapper.deleteMateCheck(ckInformationId);
        mateCheckMapper.deleteMateCheckModelById(ckInformationId);
        mateCheckMapper.removeCheckResult(ckInformationId);
        mateCheckMapper.removeCheckProduct(ckInformationId);
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("orderId", ckInformationId);
        resMap.put("orderType", "0");
        mtAuditMapper.deleteMtAuditById(resMap);
        mateTestMapper.removeTestRecord(ckInformationId);
        mateDeviceMapper.deleteUserDevice(ckInformationId);
        return AjaxResult.success("删除成功");
    }

    @GetMapping("/createWord")
    public AjaxResult createWord(@RequestParam(value = "ckInformationId", required = false) Long ckInformationId) {
        String message = mateCheckService.createWord(ckInformationId);
        if (message.equals("生成失败")) {
            return AjaxResult.error("生成Word文件失败");
        } else {
            return AjaxResult.success(message);
        }
    }

}
